﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DBA.Web.UI;
using DBA.Common;
using DBA.Model;
using DBA.Data;
using DBA.Data.ADO;

namespace DBA.Web.StudentManage
{
    public partial class EditEducationInfo : AdminPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                if (!String.IsNullOrEmpty(Request.Params["id"]))
                {
                    hidID.Value = RequestHelper.QueryString("id");
                    ModifyEducationInit();
                }
            }
        }

        private void ModifyEducationInit()
        {
            string studentInfoID = hidID.Value;
            if (!VerificationHelper.IsNullOrEmpty<string>(studentInfoID))
            {
                Em_Study studyInfo = dal.ModifyData.GetEntity<Em_Study>(studentInfoID);
                if (!VerificationHelper.IsNullOrEmpty<Em_Study>(studyInfo)) //如果学生对象不为空，那么对控件赋值
                {
                    ConvertHelper.ModelToControl<Em_Study>(studyInfo, pan_tab.Controls);
                }
                Student studentInfo = dal.ModifyData.GetEntityField<Student>(studyInfo.UserID, "Name");                
                JsHelper.RegisterStartupScript(this,"SetSelectUsers('"+studyInfo.UserID.ToString()+"','"+studentInfo.Name+"');");
            }
        }

        DataAccessLibrary dal = CommonQuery.DataAccessLibrary;

        protected void lnkSave_Click(object sender, EventArgs e)
        {
            Em_Study studyInfo = ConvertHelper.ControlToModel<Em_Study>(pan_tab.Controls);
            //设置成学生
            studyInfo.UserType = 0;
            if (hidID.Value.Equals(string.Empty))//添加学生信息
            {
                hdResult.Value = dal.ModifyData.Insert<Em_Study>(studyInfo).ToString();
            }
            else //修改学生信息
            {
                studyInfo.Id = ConvertHelper.ToInt32(hidID.Value);
                if (dal.ModifyData.Update<Em_Study>(studyInfo) == "0")
                {
                    hdResult.Value = "0";
                }
                else
                {
                    hdResult.Value = "1";
                }
            }
        }
    }
}